package com.hongyi.app.controller;

import com.github.pagehelper.PageInfo;
import com.hongyi.app.entity.User;
import com.hongyi.app.mapper.UserMapper;
import com.hongyi.app.mapper.UserNoMapper;
import com.hongyi.app.service.UserService;
import com.hongyi.app.util.Result;
import com.sun.org.apache.regexp.internal.RE;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author HongYi
 */
@RequestMapping(value = "/api/v1/user")
@Controller
@Data
@Slf4j
public class UserController {

    private UserService userService;

    /**
     * 根据Id查询用户记录
     * cli:http://localhost:8080/api/v1/user/select-by-id?user-id=6
     *
     * @param userId
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/select-by-id")
    public Result selectById(@RequestParam(value = "user-id") Integer userId) {

        System.out.println("-----hello 你好");
        log.info("接收到使用ID查询用户记录的请求");
        User user = userService.selectById(userId);
        return user.getId() > 0 ? Result.ok(user) : Result.fail();
    }

    /**
     * 用户登录
     * http://localhost:8080/api/v1/user/login?username=wangyun&password=wangyun
     *
     * @param username
     * @param password
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public Result login(@RequestParam String username, @RequestParam String password) {

        User user = userService.login(username, password);
        return user.getId() > 0 ? Result.ok(user) : Result.fail(user);
    }


    /**
     * 分页查询用户信息
     * cli: http://localhost:8080/api/v1/user/page?page=1&size=3
     *
     * @param size
     * @param page
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public Result page(@RequestParam Integer page, Integer size) {
        PageInfo<User> users = userService.paging(page, size);
        return users.getSize() > 0 ? Result.ok(users) : Result.fail();
    }

    /**
     * 通过账号查询用户记录
     * cli: http://localhost:8080/api/v1/user/check-username?username=admin
     *
     * @param username
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/check-username", method = RequestMethod.GET)
    public Result checkUsername(@RequestParam String username) {

        return userService.checkUsername(username) ? Result.ok() : Result.fail();
    }


    /**
     * 添加一条用户记录  需要的参数：username、roleId
     * cli: http://localhost:8080/api/v1/user/insert?
     *
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insert", method = RequestMethod.GET)
    public Result insert(@RequestBody User user) {
        return userService.insert(user) > 0 ? Result.ok() : Result.fail();
    }

    /**
     * 修改一条用户记录
     * cli: http://localhost:8080/api/v1/user/update-by-id?id=6&
     *
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update-by-id", method = RequestMethod.GET)
    public Result updateById(@RequestBody User user) {
        return userService.updateById(user) > 0 ? Result.ok() : Result.fail();
    }

    /**
     * 删除一条用户记录
     * cli: http://localhost:8080/api/v1/user/delete-by-id?ids=6&ids=5
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete-by-id", method = RequestMethod.GET)
    public Result deleteById(@RequestParam Integer[] id) {
        return userService.deleteByIds(id) > 0 ? Result.ok() : Result.fail();
    }
}
